const fs = require('fs')
const path = require('path')

fs.readFile(path.join(__dirname,'/index.html'),'utf-8',function(err,dataStr){
  if(err){
    return console.log('文件读取失败')
  }
  const styleReg = /<style>[\s\S]*<\/style>/g;
  const styleFile = (styleReg.exec(dataStr))[0].replace('<style>','').replace('</style>','');
  fs.writeFile(path.join(__dirname,'./clock/index.css'),styleFile, function(err){
    if(err){
      return console.log('文件写入失败index.css',err.message)
    }
  })
  const scriptReg = /<script>[\s\S]*<\/script>/g;
  const scriptFile = (scriptReg.exec(dataStr))[0].replace('<script>','').replace('</script>','');
  fs.writeFile(path.join(__dirname,'./clock/index.js'),scriptFile,function(err){
    if(err){
      return console.log('文件写入失败index.js')
    }
  })

  const newHtml = dataStr.replace(styleReg,'<link rel="stylesheet" href="./index.css"/>').replace(scriptReg,'<script src="./index.js"></script>')
  fs.writeFile(path.join(__dirname,'./clock/index.html'),newHtml,function(err){
    if(err){
      return console.log('文件写入失败index.html')
    }
  })
})